import sqlite3
from pathlib import Path


class Db:
    def __init__(self, db_file: Path):
        assert db_file.exists()
        self.db_file = db_file

    def do_request(self, request: str, mode):
        conn = None
        try:
            conn = sqlite3.connect(self.db_file)
            cursor = conn.cursor()
            if mode == 'get':
                cursor.execute(request)
                return cursor.fetchall()
            elif mode == 'post':
                cursor.execute(request)
                conn.commit()
            else:
                raise Exception(f'Undefined mode  = {mode}')
        finally:
            if conn:
                conn.close()
